#include "WindowDelegateWrapper.h"

namespace BerkeliumDotNet
{
	WindowDelegateWrapper::WindowDelegateWrapper(Window^ window)
	{
		_window = window;
	}

	void WindowDelegateWrapper::onAddressBarChanged(Berkelium::Window* win, const char* newURL, size_t newURLLength)
	{
		_window->windowDelegate->OnAddressBarChanged(_window, gcnew String(newURL));
	}

	void WindowDelegateWrapper::onStartLoading(Berkelium::Window* win, const char* newURL, size_t newURLLength)
	{
		_window->windowDelegate->OnStartLoading(_window, gcnew String(newURL));
	}

    void WindowDelegateWrapper::onLoad(Berkelium::Window* win)
	{
		_window->windowDelegate->OnLoad(_window);
    }

    void WindowDelegateWrapper::onLoadError(Berkelium::Window* win, const char* error, size_t errorLength)
	{
		_window->windowDelegate->OnStartLoading(_window, gcnew String(error));
    }
 
    void WindowDelegateWrapper::onResponsive(Berkelium::Window* win)
	{
		_window->windowDelegate->OnResponsive(_window);
    }
 
    void WindowDelegateWrapper::onUnresponsive(Berkelium::Window* win)
	{
		_window->windowDelegate->OnUnresponsive(_window);
    }
 
    void WindowDelegateWrapper::onPaint(Berkelium::Window* wini, const unsigned char* bitmap_in,
		const Rect &bitmap_rect, int dx, int dy, const Rect &scroll_rect)
	{
		_window->windowDelegate->OnPaint(_window, IntPtr((void*)bitmap_in),
			Rectangle(bitmap_rect.left(), bitmap_rect.top(), bitmap_rect.width(), bitmap_rect.height()),
			dx, dy,
			Rectangle(scroll_rect.left(), scroll_rect.top(), scroll_rect.width(), scroll_rect.height())
		);
    }
 
    void WindowDelegateWrapper::onBeforeUnload(Berkelium::Window* win, bool* proceed)
	{
		_window->windowDelegate->OnBeforeUnload(_window, *proceed);
    }

    void WindowDelegateWrapper::onCancelUnload(Berkelium::Window* win)
	{
		_window->windowDelegate->OnCancelUnload(_window);
    }

    void WindowDelegateWrapper::onCrashed(Berkelium::Window* win)
	{
		_window->windowDelegate->OnCrashed(_window);
    }
 
    void WindowDelegateWrapper::onCreatedWindow(Berkelium::Window* win, Berkelium::Window* newWindow)
	{
		_window->windowDelegate->OnCreatedWindow(_window, gcnew Window(newWindow));
    }
 
    void WindowDelegateWrapper::onChromeSend(
        Berkelium::Window* win,
        WindowDelegate::Data message,
        const WindowDelegate::Data* contents,
        size_t numContents)
    {
		_window->windowDelegate->OnChromeSend(_window, gcnew String(message.message),
			gcnew String(contents->message), numContents);
    }
 
    void WindowDelegateWrapper::onPaintPluginTexture(Berkelium::Window* win, void* sourceGLTexture,
        const std::vector<Rect> srcRects, const Rect &destRect)
	{
		List<Rectangle>^ sourceRects = gcnew List<Rectangle>(srcRects.size());
		for each (Rect rect in srcRects)
		{
			sourceRects->Add(Rectangle(rect.left(), rect.top(), rect.width(), rect.height()));
		}
		_window->windowDelegate->OnPaintPluginTexture(_window, IntPtr(sourceGLTexture), sourceRects,
			Rectangle(destRect.left(), destRect.top(), destRect.width(), destRect.height())
		);
	}
 

	void WindowDelegateWrapper::onWidgetCreated(Berkelium::Window* win, Berkelium::Widget* newWidget, int zIndex)
	{
		_window->windowDelegate->OnWidgetCreated(_window, gcnew Widget(newWidget), zIndex);
	}

    void WindowDelegateWrapper::onWidgetDestroyed(Berkelium::Window* win, Berkelium::Widget* newWidget)
	{
		_window->windowDelegate->OnWidgetDestroyed(_window, gcnew Widget(newWidget));
	}
 
    // Will be called before the first call to paint.
    void WindowDelegateWrapper::onWidgetResize(
        Berkelium::Window* win,
        Berkelium::Widget* wid,
        int newWidth,
        int newHeight)
	{
		_window->windowDelegate->OnWidgetResize(_window, gcnew Widget(wid),
			newWidth, newHeight);
	}
 
    // Never called for the main window.
    void WindowDelegateWrapper::onWidgetMove(
        Berkelium::Window* win,
        Berkelium::Widget* wid,
        int newX,
        int newY)
	{
		_window->windowDelegate->OnWidgetMove(_window, gcnew Widget(wid),
			newX, newY);
	}
 
    void WindowDelegateWrapper::onWidgetPaint(
        Berkelium::Window* win,
        Berkelium::Widget* wid,
        const unsigned char* sourceBuffer,
        const Rect &rect,
        int dx, int dy,
        const Rect &scrollRect)
	{
		_window->windowDelegate->OnWidgetPaint(_window, gcnew Widget(wid), IntPtr((void*)sourceBuffer),
			Rectangle(rect.left(), rect.top(), rect.width(), rect.height()), dx, dy,
			Rectangle(scrollRect.left(), scrollRect.top(), scrollRect.width(), scrollRect.height())
		);
	}
}